﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Mind.Data
{


    public enum LockupType
    {
        /// <summary>
        /// It's means that a property will access a 
        /// cache system to retrieve a lockup wrapper.
        /// All instances are cached in the first lockup property access
        /// </summary>
        FullCached,
        /// <summary>
        /// It's means that the wrapper will be retrieved
        /// just when the property is accessed (then it will be stored to cache to further access).
        /// </summary>
        Cached,
        /// <summary>
        /// It's means that the wrapper will be retrieved
        /// just when the property is accessed (then it will be stored in a class field to further access).
        /// </summary>
        LazyCached,
        /// <summary>
        /// It's means that the wrapper will be retrieved
        /// always when the property is accessed.
        /// </summary>
        Lazy, 
        /// <summary>
        /// When using default select methods, a join will be made to retrieve on the fly.
        /// In another cases, lazy behavior will happen.
        /// </summary>
        Joinned
       
    }

    public class LockupInfo
    {
        public ColumnInfo Column { get; private set; }
        public ISchema Schema { get; private set; }
        public ISchema ForeingSchema { get; private set; }
        public LockupType LockupType { get; set; }
        public bool IsNullable { get; private set; }
        internal Type WrapperType { get; private set; }

        internal string GenerateUniqueAlias(ColumnInfo col)
        {
            return String.Format("{0}_{1}", Column.UniqueAlias, col.UniqueAlias);
        }

        internal LockupInfo(ColumnInfo column, ISchema schema, ISchema foreingSchema, bool nullable, LockupType lockupType, Type wrapperType)
        {
            this.LockupType = lockupType;
            this.Column = column;
            this.Schema = schema;
            this.ForeingSchema = foreingSchema;
            this.IsNullable = nullable;
            this.WrapperType = wrapperType;

            //if (WrapperType.BaseType.GetGenericTypeDefinition().Equals(typeof(Sample).BaseType.GetGenericTypeDefinition()))
            //    Console.WriteLine("Feitoo");
            //else
            //    Console.WriteLine("Oh não");
        }

        internal IWrapper MakeLockupInstance()
        {
            object result = WrapperType.GetConstructor(Type.EmptyTypes).Invoke(new Object[0]);
            return result as IWrapper;
        }

      
    }
}
